package cn.doublefloat.crm.project.system.controller;

import cn.doublefloat.crm.common.TokenUtil;
import cn.doublefloat.crm.framework.web.controller.BaseController;
import cn.doublefloat.crm.framework.web.domain.JsonResult;
import cn.doublefloat.crm.framework.web.domain.TableResult;
import cn.doublefloat.crm.project.system.domain.Resource;
import cn.doublefloat.crm.project.system.domain.vo.ResourceListVo;
import cn.doublefloat.crm.project.system.domain.vo.ResourceRightVo;
import cn.doublefloat.crm.project.system.domain.vo.ResourceVo;
import cn.doublefloat.crm.project.system.domain.vo.UserVo;
import cn.doublefloat.crm.project.system.service.ResourceService;
import cn.doublefloat.crm.project.system.service.UserService;
import cn.doublefloat.crm.project.system.service.impl.UserDetailService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * TODO
 *
 * @author 李广帅
 * @version 1.0
 * @date 2021/3/28 3:15 下午
 */

@RestController
@RequestMapping("/resource")
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private UserDetailService userDetailService;

    @Autowired
    private UserService userService;

    @GetMapping("/queryResource")
    public JsonResult queryResource(Resource resource) {
        return JsonResult.success(resourceService.queryResource(resource));
    }

    @GetMapping("/getResource")
    public JsonResult findResource(HttpServletRequest request) {
        TokenUtil tokenUtil = new TokenUtil();
        Claims claims = tokenUtil.getToken(request.getHeader("token"));
        UserVo userVo = userService.findByLoginName(claims.getSubject());
        return JsonResult.success(resourceService.findResource(userVo));
    }

    @GetMapping("/getResourceList")
    public TableResult findResourceList() {
        startPage();
        List<ResourceListVo> resourceVos = resourceService.findResourceList();
        return getDataTable(resourceVos);
    }

    @GetMapping("/findRightList")
    public JsonResult findRightList() {
        List<ResourceVo> resourceVos = resourceService.findRightList(null);
        return JsonResult.success(resourceVos);
    }

    @GetMapping("/findRightList/{id}")
    public JsonResult findRoleRightList(@PathVariable Long id) {
        List<ResourceVo> resourceVos = resourceService.findRightList(id);
        return JsonResult.success(resourceVos);
    }

    @PostMapping("/save")
    public JsonResult saveResource(Resource res) {
        int i = resourceService.saveResource(res);
        return toJson(i);
    }

    @GetMapping("/{id}")
    public JsonResult findById(@PathVariable Long id) {
        Resource resource = resourceService.findById(id);
        return JsonResult.success(resource);
    }

    @DeleteMapping("/{ids}")
    public JsonResult deleteResource(@PathVariable Long[] ids) {
        System.out.println(ids[0]);
        System.out.println(ids.length);
        int i = resourceService.deleteResource(ids);
        return toJson(i);
    }
}
